#include "filerestoredialog.h"
#include "ui_filerestoredialog.h"
#include <QDebug>
#include <QFileInfo>
#include <QFileDialog>
#include "hmicommondefine.h"
#include "instructionEnum.h"


FileRestoreDialog::FileRestoreDialog(GlobalData *globalDataIn, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FileRestoreDialog)
{
    globalData=globalDataIn;
    ui->setupUi(this);
    setStyleSheet("background-color:white;");

    //uDiskLocation = USB_DISK_ADDRESS;
    //hardDiskLocation = HARD_DISK_ADDRESS;
    //restoreFileLocation=hardDiskLocation;
    restoreFileLocation = CONTROLLER_HARD_DISK_ADDRESS;
    move(0,0);
#ifdef BUILD_ARM
    setGeometry(70,128,460,595);
#endif

    //
    globalData = NULL;
}

FileRestoreDialog::~FileRestoreDialog()
{
    delete ui;
}

void FileRestoreDialog::changeLanguage()
{
    ui->retranslateUi(this);
}

void FileRestoreDialog::on_returnBtn_clicked()
{
    this->hide();
}


void FileRestoreDialog::on_restore_clicked()
{
    if(ui->lineEdit->text() == "")
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("请点击浏览按钮获取还原源文件路径"));
        return;
    }

    emit restoreFile(restoreFileLocation);
}

void FileRestoreDialog::on_exployer_clicked()
{
//    QFileInfo fileInfo;
//    QString location;
//    location=QFileDialog::getOpenFileName(this,tr(""),restoreFileLocation,tr("ZIP(*.zip);;RAR(*.rar)"));
//    fileInfo=QFileInfo(location);
//    ui->lineEdit->setText(fileInfo.filePath());
//    path=fileInfo.filePath();

    QFileDialog dialog(this,"");
    dialog.resize(580,400);
    dialog.setAcceptMode(QFileDialog::AcceptOpen);
    dialog.setNameFilter("ZIP(*.zip)");
    dialog.setDirectory(restoreFileLocation);

    QStringList files = dialog.selectedFiles();
    if(!files.isEmpty())
    {
        if(dialog.exec() == QFileDialog::Accepted)
        {
            qDebug()<<"ok";
            QStringList name = dialog.selectedFiles();
            QString line = name.join("");
            ui->lineEdit->setText(line);
            path = line;
        }
        else
        {
            qDebug()<<"close";
        }
    }
}

void FileRestoreDialog::on_radioButton_clicked()
{
    //if( 0 == system("mount | grep udisk") )//有挂载
    if( globalData->usbFlag )
    {
        qDebug()<<"exist";
        restoreFileLocation = CONTROLLER_USB_DISK_ADDRESS;
    }
    else
    {
        ui->radioButton->setChecked(false);
        ui->radioButton_2->setChecked(true);
        restoreFileLocation = CONTROLLER_HARD_DISK_ADDRESS;

        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("未检测到U盘插入"));
    }

//    QFile mFile("/agvwork/usb.data");
//    if(mFile.exists())
//    {
//        qDebug()<<"exist";
//        restoreFileLocation=uDiskLocation;
//    }
//    else
//    {
//        ui->radioButton->setChecked(0);
//        ui->radioButton_2->setChecked(1);
//        restoreFileLocation=hardDiskLocation;

//        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("未检测到U盘插入"));
//    }
}

void FileRestoreDialog::on_radioButton_2_clicked()
{
    restoreFileLocation = CONTROLLER_HARD_DISK_ADDRESS;
}

